package indi.caibo.service_edu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import indi.caibo.commonutils.JwtUtils;
import indi.caibo.commonutils.R;
import indi.caibo.commonutils.vo.UcenterMemberPay;
import indi.caibo.service_edu.controller.front.clinet.UCenterClient;
import indi.caibo.service_edu.entity.EduComment;
import indi.caibo.service_edu.entity.EduCourse;
import indi.caibo.service_edu.service.EduCommentService;
import indi.caibo.service_edu.service.EduCourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 评论 前端控制器
 * </p>
 *
 * @author Bo Cai
 * @since 2021-12-11
 */
@Api(tags = "课程评论管理")
@RestController
@RequestMapping("/edu-service/comment")
public class EduCommentController {
    @Autowired
    EduCommentService commentService;
    @Autowired
    UCenterClient uCenterClient;
    @Autowired
    EduCourseService courseService;

    //
    @ApiOperation(value = "根据课程id查询评论分页列表")
    @GetMapping("{page}/{limit}")
    public R index(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,
            @ApiParam(name = "courseId", value = "课程id", required = true)
            @RequestParam("courseId") String courseId) {
        Page<EduComment> pageParam = new Page<>(page, limit);
        QueryWrapper<EduComment> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id", courseId);
        wrapper.orderByDesc("gmt_modified");
        commentService.page(pageParam, wrapper);
        List<EduComment> commentList = pageParam.getRecords();
        Map<String, Object> map = new HashMap<>();
        map.put("items", commentList);
        map.put("current", pageParam.getCurrent());
        map.put("pages", pageParam.getPages());
        map.put("size", pageParam.getSize());
        map.put("total", pageParam.getTotal());
        map.put("hasNext", pageParam.hasNext());
        map.put("hasPrevious", pageParam.hasPrevious());
        return R.ok().data(map);
    }

    @ApiOperation(value = "添加评论")
    @PostMapping("save")
    public R save(@RequestBody EduComment comment, HttpServletRequest request) {
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        if (StringUtils.isEmpty(memberId)) {
            return R.error().code(28004).message("请登录");
        }
        comment.setMemberId(memberId);
        UcenterMemberPay uCenterInfo = uCenterClient.getInfo(memberId);
        comment.setNickname(uCenterInfo.getNickname());
        comment.setAvatar(uCenterInfo.getAvatar());
        commentService.save(comment);
        courseService.addCommentCount(comment.getCourseId());
        return R.ok();
    }

    @ApiOperation(value = "删除评论")
    @DeleteMapping("delete")
    public R delete(@RequestBody EduComment comment, HttpServletRequest request) {
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        if (StringUtils.isEmpty(memberId)) {
            return R.error().code(28004).message("请登录");
        }
        if(!memberId.equals(comment.getMemberId())) {
            return R.error().message("删除失败，此评论不是您发布的！");
        }
        courseService.subCommentCount(comment.getCourseId());  // 此课程评论量减一
        commentService.deleteCommentCountById(comment.getId());
        return R.ok();
    }
}

